// package throughtester 用 a->b 的方式测试网络链路的连通性
package throughtester

import (
	"net"
	"time"

	"github.com/google/gopacket/layers"

	"gitee.com/sillyman/pcaphelper"
	"gitee.com/sillyman/pcaphelper/pktbuilder"
)

// throughTester
type throughTester struct {
	tx *pcaphelper.PcapHelper
	rx *pcaphelper.PcapHelper

	// LearnDstFirst 让网络先学习目标的 MAC 地址
	LearnDstFirst bool
}

// New Create an instance
func New(tx, rx *pcaphelper.PcapHelper, learnFirst bool) *throughTester {
	return &throughTester{tx, rx, learnFirst}
}

// maybeLetNetworkLearnMe 当 LearnDstFirst=true 就让网络设备先学习目标MAC地址
func (t *throughTester) maybeLetNetworkLearnMe(src net.HardwareAddr) {
	if !t.LearnDstFirst {
		return
	}

	for i := 0; i < 2; i++ {
		_ = t.rx.WritePacketData(pktbuilder.BuildingEtherPkt(src, layers.EthernetBroadcast, nil))
	}

	time.Sleep(10 * time.Millisecond)
}
